//
//  WeiboCell.m
//  Weibo
//
//  Created by taoshanchang on 15/1/29.
//  Copyright (c) 2015年 taoshanchang. All rights reserved.
//

#import "WeiboCell.h"
#import "UIImageView+WebCache.h"
#import "UIViewExt.h"
#import "UIUtils.h"
#import "RegexKitLite.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height   //屏幕的高度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width     //屏幕的宽度

@implementation WeiboCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self _initView];
    }
    return self;

}

-(void)_initView{
    
    //用户头像
    _userImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    _userImage.backgroundColor = [UIColor clearColor];
    _userImage.layer.cornerRadius = 5;
    _userImage.layer.borderWidth = .5;
    _userImage.layer.borderColor = [UIColor grayColor].CGColor;
    _userImage.layer.masksToBounds = YES;
    [self.contentView addSubview:_userImage];
    
    //昵称
    _nickLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _nickLabel.backgroundColor = [UIColor clearColor];
    _nickLabel.font = [UIFont systemFontOfSize:12.0f];
    [self.contentView addSubview:_nickLabel];
    
    //转发数
    _repostCountLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _repostCountLabel.font = [UIFont systemFontOfSize:12.0f];
    _repostCountLabel.backgroundColor = [UIColor clearColor];
    _repostCountLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_repostCountLabel];
    
    //回复数
    _commentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _commentLabel.font = [UIFont systemFontOfSize:12.0f];
    _commentLabel.backgroundColor = [UIColor clearColor];
    _commentLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_commentLabel];

    //微博来源
    _sourceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _sourceLabel.font = [UIFont systemFontOfSize:12.0f];
    _sourceLabel.backgroundColor = [UIColor clearColor];
    _sourceLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_sourceLabel];
    
    //发布时间
    _createTimeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _createTimeLabel.font = [UIFont systemFontOfSize:12.0f];
    _createTimeLabel.backgroundColor = [UIColor clearColor];
    _createTimeLabel.textColor = [UIColor blueColor];
    [self.contentView addSubview:_createTimeLabel];
    
    //设置cell选中时的背景颜色
    _weiboView = [[WeiboView alloc] initWithFrame:CGRectZero];
    [self addSubview:_weiboView];
    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    selectedBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"statusdetail_cell_sepatator"]];
    self.selectedBackgroundView = selectedBackgroundView;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    //用户头像
    _userImage.frame = CGRectMake(5, 5, 35,35);
    NSString *userImageUrl = _weiboModel.user.profile_image_url;
    [_userImage sd_setImageWithURL:[NSURL URLWithString:userImageUrl]];
    
    //用户昵称
    _nickLabel.frame = CGRectMake(50, 5, 200, 20);
    _nickLabel.text = _weiboModel.user.screen_name;
    
    //微博视图
    _weiboView.weiboModel = _weiboModel;
    
    float h = [WeiboView getWeiboViewHeight:_weiboModel isRepost:NO isDetail:NO];
    _weiboView.frame = CGRectMake(50, _nickLabel.bottom, kWeibo_List_width, h);
    //调用微博view的重新布局方法
    [_weiboView setNeedsLayout]; 
    
    //发布时间
    NSString *strCreateTime = _weiboModel.createData;
    if(strCreateTime != nil){
        _createTimeLabel.hidden = NO;
        NSString *date = [UIUtils fomateString:strCreateTime];
        _createTimeLabel.text = date;
        _createTimeLabel.frame = CGRectMake(50, self.height-20, 100, 20);
        [_createTimeLabel sizeToFit];
    }else{
        _createTimeLabel.hidden = YES;
    }
    
    //微博来源
    NSString *source = _weiboModel.source;
    if(source != nil){
        _sourceLabel.hidden = NO;
        NSString *from = [self parseSource:source];
        _sourceLabel.text = [NSString stringWithFormat:@"来自%@",from];
        _sourceLabel.frame = CGRectMake(_createTimeLabel.right+20, self.height-20, 100, 20);
        [_sourceLabel sizeToFit];
    }else{
        _sourceLabel.hidden = YES;
    }

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(NSString *)parseSource:(NSString *)source{
    NSString *regex = @">\\w+( )?\\w+(.)?\\w+<";
    NSArray *array = [source componentsMatchedByRegex:regex];
    if(array.count > 0){
        NSString *ret = [array objectAtIndex:0];
        NSRange range;
        range.location = 1;
        range.length = ret.length - 2;
        NSString *resultString = [ret substringWithRange:range];
        return resultString;
    }
    return nil;
}

@end
